package view.backend.graveyard;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.GraveYard;
import model.handlers.GraveYardHandler;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminGraveYardOverview 
{
    private JPanel overviewPanel;
    private AdminGraveYard adminGraveYard;
    private JLabel
            lblName,
            lblStreet,
            lblZipCity,
            lblAlreadyExist;
    private JButton btnAddAlter;

    public AdminGraveYardOverview(AdminGraveYard view)
    {
        adminGraveYard = view;
        
        overviewPanel = new JPanel(null);
        overviewPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Oversigt", TitledBorder.DEFAULT_JUSTIFICATION, 
                                    TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        overviewPanel.setBounds(310, 0, 340, 150);
        overviewPanel.setOpaque(false);
        
        lblName = new JLabel("");
        lblName.setBounds(10, 35, 290, 20);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        lblStreet = new JLabel("");
        lblStreet.setBounds(10, 55, 290, 20);
        lblStreet.setFont(DesignLayout.STANDARD);
        
        lblZipCity = new JLabel("");
        lblZipCity.setBounds(10, 75, 290, 20);
        lblZipCity.setFont(DesignLayout.STANDARD);
        
        btnAddAlter = new JButton("tilføj");
        btnAddAlter.setBounds(200, 105, 130, 35);
        btnAddAlter.setEnabled(false);
        btnAddAlter.setBorder(DesignLayout.T_FIELD_RED);
        btnAddAlter.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnAddAlter.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                boolean addToOrder = false;
                try                
                {
                    if (GraveYardHandler.doesGraveYardExistOnAddress(adminGraveYard.getGraveYardObject()) == true)                    
                    {
                        addToOrder = true;
                    }
                }
                catch (SQLException ex)
                {
                    ExceptionManager.handleSQLException(ex);
                } 
                catch (ClassNotFoundException ex)
                {
                    ExceptionManager.handleClassNotFoundException(ex);
                }
                
                if (addToOrder == true)
                {
                    if (ViewSingleton.getSingletonObject().getOrderGraveYard() != null)
                    {
                        ViewHandler.toggleAllsView("adminGraveYard", false);
                        ViewHandler.toggleAllsView("menuLeft", false);
                        ViewSingleton.getSingletonObject().getMenus()[0].resetBackgroundColor(DesignLayout.T_FIELD_BORDER_RED);
                        ViewHandler.toggleAllsView("orderContainer", true);
                        GraveYard[] graveYard = { adminGraveYard.getGraveYardObject() };
                        ViewSingleton.getSingletonObject().getOrderGraveYard().getOrder().getOrderObject().getTombStone().setGraveYard(graveYard[0]);
                        ViewSingleton.getSingletonObject().getOrderGraveYard().setFieldsFromModel();
                        ViewSingleton.getSingletonObject().getOrderGraveYard().buildFoundPanel(graveYard);                    
                    }
                }
                else
                {
                    try
                    {
                        int added = control.GraveYardControl.addGraveYard(adminGraveYard.getGraveYardObject());
                        if (added > 0)
                        {
                            JOptionPane.showMessageDialog(null,
                            "Kirkegården blev tilføjet",
                            "Succes",
                            JOptionPane.INFORMATION_MESSAGE);

                            adminGraveYard.reset();
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
            }
        });
        
        lblAlreadyExist = new JLabel("Eksisterer allerede");
        lblAlreadyExist.setForeground(Color.red);
        lblAlreadyExist.setBounds(10, 105, 150, 35);
        lblAlreadyExist.setVisible(false);        
        
        overviewPanel.add(lblAlreadyExist);
        overviewPanel.add(lblName);
        overviewPanel.add(lblStreet);
        overviewPanel.add(lblZipCity);
        overviewPanel.add(btnAddAlter);
        overviewPanel.setVisible(false);
    }
    
    public void uddateLabelsFromModel()
    {
        GraveYard model = adminGraveYard.getGraveYardObject();
        lblName.setText(model.getName());
        lblStreet.setText(model.getAddress().getStreet());
        lblZipCity.setText(model.getAddress().getZipCode().getZipCode() + " " + model.getAddress().getZipCode().getCity());
        
        if (model.isCompleted() == true && model.getAddress().isCompleted())
        {
            try
            {
                model = GraveYardHandler.getExistingGraveYard(model);
                
                if (GraveYardHandler.doesGraveYardExistOnAddress(model) == true)                
                {
                    if (ViewSingleton.getSingletonObject().getOrderGraveYard() != null)
                    {
                        btnAddAlter.setEnabled(true);
                        btnAddAlter.setText("tilføj til ordre");
                        lblAlreadyExist.setVisible(false);
                    }
                    else
                    {
                        btnAddAlter.setEnabled(false);
                        btnAddAlter.setText("tilføj");
                        lblAlreadyExist.setText("Eksisterer allerede");
                        lblAlreadyExist.setVisible(true);
                    }
                    
                }
                else
                {
                    btnAddAlter.setEnabled(true);
                    btnAddAlter.setText("tilføj");
                    btnAddAlter.setForeground(Color.BLACK);
                    lblAlreadyExist.setVisible(false);                    
                }
            } 
            catch (SQLException ex)
            {
                ExceptionManager.handleSQLException(ex);
            } 
            catch (ClassNotFoundException ex)
            {
                ExceptionManager.handleClassNotFoundException(ex);
            }
        }
        else
        {
            btnAddAlter.setEnabled(false);
            btnAddAlter.setForeground(Color.BLACK);
        }
    }

    public JPanel getPanel()
    {
        return overviewPanel;
    }
}